package com.shallwead.bna.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shallwead.bna.BnA;
import com.shallwead.bna.R;
import com.shallwead.bna.adapter.viewholder.AdBackListViewHolder;
import com.shallwead.bna.object.AdBack;
import com.shallwead.bna.util.image.ImageDownloader;

public class AdBackListAdapter extends BaseAdapter {

	Context mContext = null;
	LayoutInflater inflater = null;
	ArrayList<AdBack> MyList = null;
	AdBackListViewHolder viewHolder = null;

	public AdBackListAdapter(Context context, ArrayList<AdBack> MyList) {

		super();
		mContext = context;
		this.inflater = LayoutInflater.from(context);
		this.MyList = MyList;

	}

	@Override
	public int getCount() {
		return MyList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final int pos = position;
		viewHolder = new AdBackListViewHolder();
		convertView = null;
		View v = convertView;

		if (v == null) {
			v = inflater.inflate(R.layout.layout_adback_item, parent, false);
			viewHolder.layout = (RelativeLayout) v
					.findViewById(R.id.adback_item_layout);
			viewHolder.title = (TextView) v
					.findViewById(R.id.adback_item_title);
			viewHolder.icon = (ImageView) v.findViewById(R.id.adback_item_icon);
			
		} else {
			viewHolder = (AdBackListViewHolder) v.getTag();
		}
		final AdBack adback = MyList.get(pos);
		
		
		BnA.imgDownloader.download(adback.getImageUrl(), viewHolder.icon);
		
		
		viewHolder.title.setText(adback.getName());
		viewHolder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BnA.goAd(mContext, adback);
			}
		});

		return v;
	}

}